// ship.cpp

#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include "ship.h"
#include <list>
using namespace std;

ship::ship( float _x, float _y ){
	x = _x;
	y = _y;
	num_shot = 0;
	max_shot = 3;
}

void ship::move_to_xy( float _x, float _y){
	x = _x;
	y = _y;
}

int ship::move_xy( float _x, float _y,
		float maxx, float minx,
		float maxy, float miny ){

	if( x+_x < maxx && x+_x > minx )
		x += _x;
	else
		return 1;

	if( y+_y < maxy && y+_y > miny )
		y += _y;
	else
		return 1;

	return 0;
}

void ship::fire(){

	if( num_shot < max_shot ){
		num_shot++;
		bullet* bul = new bullet(x,y);
		b.push_back( bul );
	}
}

void ship::render(){
	glLoadIdentity();

	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, textureID);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

	glTranslatef( x, y, 0.0f);
	glScalef(0.12f, 0.12f, 0.12f);

	glPushMatrix();
	glBegin(GL_QUADS);

	glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f);
	glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 0.0f);
	glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 0.0f);
	glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);

	glEnd();
	glPopMatrix();

	glDisable(GL_TEXTURE_2D);
}

void ship::print_atributes(){
	printf( "%.2f %.2f %d %d\n", x, y, num_shot, max_shot );
}

/*---------------------------------------------------------------------------*/

bullet::bullet(float _x, float _y ){

	move_to_xy( _x, _y );
}

void bullet::render() {
	glLoadIdentity();

	glTranslatef( x, y, 0.0f);
	glScalef(0.01f, 0.01f, 0.01f);

	glPushMatrix();

	glBegin(GL_TRIANGLES);

	glVertex3f( 1.0,0.0,0.0);
	glVertex3f( 0.0,1.0,0.0);
	glVertex3f(-1.0,0.0,0.0);

	glEnd();
	glPopMatrix();
}	

void bullet::doubleRender(){
	glLoadIdentity();

	glTranslatef(0.105f+x, y, 0.0f);
	glScalef(0.01f, 0.01f, 0.01f);

	glPushMatrix();

	glBegin(GL_TRIANGLES);

	glVertex3f( 1.0,0.0,0.0);
	glVertex3f( 0.0,1.0,0.0);
	glVertex3f(-1.0,0.0,0.0);

	glEnd();
	glPopMatrix();

	glLoadIdentity();

	glTranslatef(-0.105f+x, y, 0.0f);
	glScalef(0.01f, 0.01f, 0.01f);

	glPushMatrix();

	glBegin(GL_TRIANGLES);

	glVertex3f( 1.0,0.0,0.0);
	glVertex3f( 0.0,1.0,0.0);
	glVertex3f(-1.0,0.0,0.0);

	glEnd();
	glPopMatrix();

}

int bullet::move_xy( float _x, float _y,
		float maxx, float minx,
		float maxy, float miny ){

	if( x+_x < maxx && x+_x > minx )
		x += _x;
	else
		return 1;

	if( y+_y < maxy && y+_y > miny )
		y += _y;
	else
		return 1;

	return 0;
}

void bullet::move_to_xy( float _x, float _y){
	x = _x;
	y = _y;
}
